package de.koller.caldera.render.planets;

import de.koller.aframe.gl.FileShaderProgram;
import de.koller.caldera.CNode;
import de.koller.caldera.engine.planet.Planet;
import de.koller.oceanFramework.gl.util.OceanShader;
import de.koller.oceanFramework.gl.util.ViewUtil;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

public class PlanetRendering extends CNode {
	
	private OceanShader shader;
	private float rot = 0;

	@Override
	protected void update( int elapsedTime ) {
		rot += 0.02 * elapsedTime;
	}
	
	
	
	@Override
	protected void init() {
		super.init();
		
		shader = OceanShader.createShader( new FileShaderProgram( 
				"de/koller/caldera/render/planets/planetSurface.vert", 
				"de/koller/caldera/render/planets/planetSurface.frag" ) );
	}
	
	public void renderSurfaces() {
		GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP );
		GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP );
		
		for( Planet p : croot.engine.planets )
			renderSurface( p );
	}
	
	public void renderAtmospheres() {
		
	}
	
	private void renderSurface( Planet p ) {
		shader.useShader();
		
		GL13.glActiveTexture( GL13.GL_TEXTURE1 );
		p.getBumpTexture().bindMipMap();
		
		GL13.glActiveTexture( GL13.GL_TEXTURE0 );
		p.getSurfaceTexture().bindMipMap();
		
		int t1 = ARBShaderObjects.glGetUniformLocationARB( shader.getProgramID(), "texture0" );
		ARBShaderObjects.glUniform1iARB( t1, 0 );
		
		int t2 = ARBShaderObjects.glGetUniformLocationARB( shader.getProgramID(), "texture1" );
		ARBShaderObjects.glUniform1iARB( t2, 1 );
		
		int lv = ARBShaderObjects.glGetUniformLocationARB( shader.getProgramID(), "lightVector" );
		ARBShaderObjects.glUniform1fARB( lv, -rot );
		
		GL11.glPushMatrix();
		GL11.glTranslatef( p.position.x, p.position.y, 0 );
		GL11.glRotatef( rot, 0, 0, 1 ); 
	
		ViewUtil.draw2D( -p.radius, -p.radius, p.radius*2f, p.radius*2f, 0 );
		GL11.glPopMatrix();
		
		shader.unuseShader();
	}
	
}
